package com.web.news.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.web.news.mapper.CommentsMapper;
import com.web.news.mapper.NewsCustomMapper;
import com.web.news.mapper.NewsMapper;
import com.web.news.mapper.TopicMapper;
import com.web.news.pojo.Comments;
import com.web.news.pojo.CommentsExample;
import com.web.news.pojo.News;
import com.web.news.pojo.NewsDo;
import com.web.news.pojo.Topic;
import com.web.news.service.NewsService;
import com.web.news.utils.MyBatisUtil;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;

public class NewsServiceImpl implements NewsService {
	
	@Override
	public List<News> findAllNews() {
		// TODO Auto-generated method stub
		SqlSession session = MyBatisUtil.getSession();
		session.clearCache();
		NewsMapper newsMapper = session.getMapper(NewsMapper.class);
		List<News> list = newsMapper.selectByExample(null);
		System.out.println("session======:" + session);
		MyBatisUtil.closeSession(session);
		return list;
	}

	@Override
	public NewsDo findNewDetailById(int nid) {
		// TODO Auto-generated method stub
		SqlSession session = MyBatisUtil.getSession();
		NewsCustomMapper newsCustomMapper = session.getMapper(NewsCustomMapper.class);
		NewsDo newsDo = newsCustomMapper.findNewsDetailById(nid);
		//session.close();
		MyBatisUtil.closeSession(session);
		return newsDo;
	}

	@Override
	public List<Comments> findCommentsByNews(int nid) {
		SqlSession session = MyBatisUtil.getSession();
		CommentsMapper commentsMapper = session.getMapper(CommentsMapper.class);
		CommentsExample example = new CommentsExample();
		CommentsExample.Criteria criteria = example.createCriteria();
		criteria.andCnidEqualTo(nid);
		List<Comments> list = commentsMapper.selectByExample(example);
		MyBatisUtil.closeSession(session);
		return list;
	}

	@Override
	public void addComments(Comments comments) {
		SqlSession session = MyBatisUtil.getSession();
		String cacheName = NewsMapper.class.getName();
		Ehcache cache = CacheManager.create().getEhcache(cacheName);
		cache.removeAll(); 
		CommentsMapper commentsMapper = session.getMapper(CommentsMapper.class);
		
		comments.setCdate(new Date());
		commentsMapper.insert(comments);
		session.commit();
		
		MyBatisUtil.closeSession(session);
	}

	@Override
	public void addNews(News news) {
		SqlSession session = MyBatisUtil.getSession();
		NewsMapper newsMapper = session.getMapper(NewsMapper.class);
		newsMapper.insert(news);
		session.commit();
		MyBatisUtil.closeSession(session);
	}

	@Override
	public List<Topic> findAllTopic() {
		SqlSession session = MyBatisUtil.getSession();
		TopicMapper topicMapper = session.getMapper(TopicMapper.class);
		List<Topic> list = topicMapper.selectByExample(null);
		MyBatisUtil.closeSession(session);
		return list;
	}

	@Override
	public void saveNews(News news) {
		// TODO Auto-generated method stub
		SqlSession session = MyBatisUtil.getSession();
		NewsMapper newsMapper = session.getMapper(NewsMapper.class);
		newsMapper.updateByPrimaryKey(news);
		session.commit();
		MyBatisUtil.closeSession(session);
	}

	@Override
	public void deleteComments(Integer  cnid) {
		// TODO Auto-generated method stub
		SqlSession session = MyBatisUtil.getSession();
		CommentsMapper commentsMapper = session.getMapper(CommentsMapper.class);
		commentsMapper.deleteByPrimaryKey(cnid);
		session.commit();
		MyBatisUtil.closeSession(session);
	}

	@Override
	public void deleteNews(Integer nid) {
		// TODO Auto-generated method stub
		SqlSession session = MyBatisUtil.getSession();
		NewsMapper newsMapper = session.getMapper(NewsMapper.class);
		newsMapper.deleteByPrimaryKey(nid);
		session.commit();
		MyBatisUtil.closeSession(session);
	}




	
	

}
